﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using LotteryAdmin.AdminService;

namespace LotteryAdmin
{
    public partial class ManualLottery : UserControl
    {
        private int currentPos = 0;
        AdminService.LotteryAdminClient client = WcfProxy.GetProxy;
        public ManualLottery()
        {
            InitializeComponent();
            dtFrom.SelectedDate = DateTime.Now;
            dtTo.SelectedDate = DateTime.Now;
            client.GetLotteryNumsInfoListCompleted += new EventHandler<AdminService.GetLotteryNumsInfoListCompletedEventArgs>(client_GetLotteryNumsInfoListCompleted);
            client.GetLotteryNumsInfoListAsync(txtLotteryNums.Text.Trim(), dtFrom.SelectedDate.Value, dtTo.SelectedDate.Value, cbxLotteryType.SelectedIndex+1);
            loadingProgress.IsBusy = true;
            this.KeyDown += new KeyEventHandler(Page_KeyDown);
        }
        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == 13)
                this.btnSearch_Click(this.btnSearch, null);
        }

        private void client_GetLotteryNumsInfoListCompleted(object sender, AdminService.GetLotteryNumsInfoListCompletedEventArgs e)
        {
            loadingProgress.IsBusy = false;
            if (e.Result.Count == 0)
            {
                MessageBox.Show("没有找到匹配的记录!");
            }
            currentPos = 0;
            PagedCollectionView pcv = new PagedCollectionView(e.Result);
            dpList.Source = pcv;
            dgList.ItemsSource = pcv;
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if(!Validate.IsZNumber(txtLotteryNums.Text.Trim()))
            {
                MessageBox.Show("期号应该是全数字!");
                return;
            }
            loadingProgress.IsBusy = true;
            client.GetLotteryNumsInfoListAsync(txtLotteryNums.Text.Trim(), dtFrom.SelectedDate.Value, dtTo.SelectedDate.Value, cbxLotteryType.SelectedIndex + 1);
        }

        private void btnAddLotteryNums_Click(object sender, RoutedEventArgs e)
        {
            new ChildAddLotteryNums().Show();
        }

        private void dgList_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            currentPos += 1;
            if((e.Row.DataContext as LotteryNumsInfo).LotteryNums==null
                || (e.Row.DataContext as LotteryNumsInfo).LotteryNums.Trim()==string.Empty)
                (e.Row.DataContext as LotteryNumsInfo).LotteryNums = "未开奖";
            (dgList.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (dpList.PageSize * dpList.PageIndex+e.Row.GetIndex()+1).ToString();
        }

        private void cbxLotteryType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.RemovedItems.Count>0)
            {
                loadingProgress.IsBusy = true;
                client.GetLotteryNumsInfoListAsync(txtLotteryNums.Text.Trim(), dtFrom.SelectedDate.Value, dtTo.SelectedDate.Value, cbxLotteryType.SelectedIndex + 1); 
            }
        }

        private void btnPaijiang_Click(object sender, RoutedEventArgs e)
        {
            new ChildPaijiang().Show();
        }
    }
}
